package com.srd.phone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengrongdai.srd.R;
import com.srd.phone.adapter.NoticeListAdapter;
import com.srd.phone.bean.NoticeInfo;
import com.srd.phone.mvp.presenters.BasePresenter;

import java.util.ArrayList;
import java.util.List;

public class NoticeListActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener {

    private PullToRefreshListView lv_list;

    private String url = "http://192.168.1.224:8080/api/a/GetArtic"; //获取文章公告
    private int page = 1;
    private NoticeListAdapter adapter;
    private List<NoticeInfo> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_list);
        initView();
        initData();
    }

    @Override
    public void initView() {
        super.initView();
        tv_common_title.setText("公告");
        lv_list = (PullToRefreshListView) findViewById(R.id.lv_list);
        lv_list.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        lv_list.setOnRefreshListener(this);
        adapter = new NoticeListAdapter(mContext);
        lv_list.setAdapter(adapter);
        lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, NoticeDetailActivity.class);
                intent.putExtra("id", list.get(position - 1).id);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new BasePresenter(requestQueue, this);
        setUrlArrs(url);
        showWaitingDialog();
        getData();
    }

    public void getData() {
        url += "?siteId=4&stat=2&pageNo=" + page;
        presenter.requestGetData(url);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        cancelWaitingDialog();
        lv_list.onRefreshComplete();
        List<NoticeInfo> arrList = NoticeInfo.parser(data);
        list.addAll(arrList);
        adapter.setData(list);
        if (arrList.size() != 0) {
            page++;
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase refreshView) {
        getData();
    }
}
